package top.codeora.aiollama.tools;

import cn.hutool.core.io.FileUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import top.codeora.aiollama.constant.FileConstant;


@Slf4j
public class FileOperationTool {

    private final String FILE_DIR = FileConstant.FILE_SAVE_DIR + "/file";

    @Tool(description = "Read content from a file",name = "文件读工具")
    public String readFile(@ToolParam(description = "Name of a file to read") String fileName) {
        String filePath = FILE_DIR + "/" + fileName;
        try {
            return FileUtil.readUtf8String(filePath);
        } catch (Exception e) {
            log.error("读取文件失败: {}", fileName, e);
            return "Error reading file" + e.getMessage();
        }

    }


    @Tool(description = "Write content from a file",name = "文件写工具")
    public String writeFile(@ToolParam(description = "Name of a file to write") String fileName,
                            @ToolParam(description = "Content to write to the file") String content) {
        String filePath = FILE_DIR + "/" + fileName;
        try {
            //创建目录
            FileUtil.mkdir(FILE_DIR);
            FileUtil.writeUtf8String(content, filePath);
            return "File written successfully to" + filePath;
        } catch (Exception e) {
            log.error("写入文件失败: {}", fileName, e);
            return "Error writing file" + e.getMessage();
        }
    }
}
